package mysql

import "project/pkg/models"

func (m Mysql) TeacherCheckExist(username string) (exist bool, info *models.TeacherInfo, err error) {
	var count int64
	exist = false
	err = m.db.Table("teacher_info").Where("username = ?", username).Count(&count).Error
	if err != nil {
		return
	}
	if count > 0 {
		exist = true
		err = m.db.Table("teacher_info").Where("username = ?", username).First(&info).Error
		if err != nil {
			return
		}
	}
	return

}
func (m Mysql) TeacherCheckExistByID(id string) (exist bool, info *models.TeacherInfo, err error) {
	var count int64
	exist = false
	err = m.db.Table("teacher_info").Where("id = ?", id).Count(&count).Error
	if err != nil {
		return
	}
	if count > 0 {
		exist = true
		err = m.db.Table("teacher_info").Where("id = ?", id).First(&info).Error
		if err != nil {
			return
		}
	}
	return
}
func (m Mysql) TeacherUpdateInfoBuID(val *models.TeacherInfo) (err error) {
	return m.db.Table("teacher_info").Save(&val).Error
}
func (m Mysql) TeacherDeleteByID(username string) (err error) {
	return m.db.Table("teacher_info").Unscoped().Where("username = ?", username).Delete(&models.TeacherInfo{}).Error
}
func (m Mysql) TeacherQueryGrade(grade int) (res []*models.TeacherInfo, count int64, err error) {
	err = m.db.Table("teacher_info").Where("grade = ?", grade).Count(&count).Find(&res).Error
	return
}
func (m Mysql) TeacherQueryTeacherID(ID string) (res *models.TeacherInfo, err error) {
	err = m.db.Table("teacher_info").Where("id = ?", ID).First(&res).Error
	return
}
